/*
 * Copyright (C) 2015 Bilibili
 * Copyright (C) 2015 Zhang Rui <bbcallen@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mzplayer.widget;

import android.annotation.SuppressLint;
import android.graphics.SurfaceTexture;
import android.os.Build;
import android.view.Surface;
import android.view.TextureView;

import com.mzplayer.utils.MeasureHelper;
import com.mzplayer.videoview.base.BaseParent;

@SuppressLint("ViewConstructor")
public class TextureRenderView extends TextureView implements TextureView.SurfaceTextureListener {
    private final BaseParent baseParent;
    private final MeasureHelper measureHelper;
    private SurfaceTexture surfaceTexture;

    public TextureRenderView(BaseParent baseParent) {
        super(baseParent.getContext());
        this.baseParent = baseParent;
        this.measureHelper = new MeasureHelper();
        setSurfaceTextureListener(this);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        this.measureHelper.doMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(this.measureHelper.getMeasuredWidth(), this.measureHelper.getMeasuredHeight());
    }

    public void setVideoRotation(int degree) {
        this.measureHelper.setVideoRotation(degree);
        setRotation(degree);
    }

    public void setVideoSize(int width, int height) {
        this.measureHelper.setVideoSize(width, height);
        requestLayout();
    }

    public int getVideoWidth() {
        return this.measureHelper.getVideoWidth();
    }

    public int getVideoHeight() {
        return this.measureHelper.getVideoHeight();
    }

    public void release() {
        if (null != this.surfaceTexture) {
            this.surfaceTexture.release();
            this.surfaceTexture = null;
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        if (this.surfaceTexture != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                setSurfaceTexture(this.surfaceTexture);
                return;
            }
        }
        this.surfaceTexture = surfaceTexture;
        this.baseParent.setSurface(new Surface(this.surfaceTexture));
        requestLayout();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        if(getWindowVisibility()==INVISIBLE) {
            release();
        }
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}
